#include "OpenGLBuffer.h"
#include "hzpch.h"

#include <glad/glad.h>

namespace Hazel
{
/////////////////////////////////////////////////////////////////////////////////////////////
// Vertex Buffer ////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

OpenGLVertexBuffer::OpenGLVertexBuffer(float *vertices, uint32_t size)
{
    HZ_PROFILE_FUNC();

    // Create Vertex Buffer Object
    glCreateBuffers(1, &m_RendererID);          
    glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
    // Copy Vertex Data To Buffer
    glBufferData(GL_ARRAY_BUFFER, size, vertices,
                 GL_STATIC_DRAW); 
}
OpenGLVertexBuffer::OpenGLVertexBuffer(uint32_t size)
{
    HZ_PROFILE_FUNC();
        glCreateBuffers(1, &m_RendererID);          
    glBindBuffer(GL_ARRAY_BUFFER, m_RendererID); 
    // Copy Vertex Data To Buffer
    glBufferData(GL_ARRAY_BUFFER, size, nullptr,
                 GL_DYNAMIC_DRAW); 

}
OpenGLVertexBuffer::~OpenGLVertexBuffer()
{
    HZ_PROFILE_FUNC();

    // Delete Vertex Buffer Object
    glDeleteBuffers(1, &m_RendererID);
}
void OpenGLVertexBuffer::Bind() const
{
    HZ_PROFILE_FUNC();

    // Bind Vertex Buffer Object
    glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
}
void OpenGLVertexBuffer::Unbind() const
{
    HZ_PROFILE_FUNC();

    // Unbind Vertex Buffer Object
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void OpenGLVertexBuffer::SetData(const void *data, uint32_t size)
{
    glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
    glBufferSubData(GL_ARRAY_BUFFER, 0, size, data);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// Index Buffer ////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////

OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t *indices, uint32_t count) : m_Count(count)
{
    HZ_PROFILE_FUNC();

    // Create Index Buffer Object
    glCreateBuffers(1, &m_RendererID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
    // Copy Index Data To Buffer
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(uint32_t), indices, GL_STATIC_DRAW);
}
OpenGLIndexBuffer::~OpenGLIndexBuffer()
{
    HZ_PROFILE_FUNC();

    // Delete Index Buffer Object
    glDeleteBuffers(1, &m_RendererID);
}
void OpenGLIndexBuffer::Bind() const
{
    HZ_PROFILE_FUNC();

    // Bind Index Buffer Object
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
}
void OpenGLIndexBuffer::Unbind() const
{
    HZ_PROFILE_FUNC();

    // Unbind Index Buffer Object
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
} // namespace Hazel